package com.jiuqi.common.authority.netrep.origin.util;

import java.util.*;

public class StringList implements java.io.Serializable
{
  protected List items = new ArrayList();
  protected List objects = null;

  public boolean isEmpty(){
    return items.size()==0;
  }

  public int size()
  {
    return items.size();
  }

  public void clear()
  {
    items.clear();
    if(objects!=null) objects.clear();
  }

  public String get(int index)
  {
    return (String)items.get(index);
  }

  public void set(int index, String newValue)
  {
    items.set(index, newValue);
  }

  public Object getObject(int index){
    return objects == null ? null : objects.get(index);
  }
  public Object getObject(String item){
    if(objects == null)
      return null;
    else{
      int index = indexOf(item);
      return index < 0 ? null : objects.get(index);
    }
  }

  public void setObject(int index, Object value){
    if(value==null && objects==null) return;
    if( objects == null ){
      objects = new ArrayList(size());
      for (int i=0; i< size(); i++)
        objects.add(null);
    }
    objects.set(index, value);
  }

  public void setObject(String item, Object value){
    int index = indexOf(item);
    if (index < 0) {
      add(item);
      index = size() -1;
    }
    setObject(index, value);
  }

  public void assign(StringList source) {
    if (source.items == null) {
      if (items != null) items.clear();
    } else {
      if (items == null) items = new ArrayList();
      else items.clear();
      items.addAll(source.items);
    }
    if (source.objects == null) {
      if (objects != null) objects.clear();
    } else {
      if (objects == null) objects = new ArrayList();
      else objects.clear();
      objects.addAll(source.objects);
    }
  }
  public void add(String item)
  {
    items.add(item);
    if(objects!=null) objects.add(null);
  }

  public void add(String item, Object data)
  {
    items.add(item);
    if(objects!=null) objects.add(null);
    setObject(items.size() - 1, data);
  }

  public void add(int index, String item)
  {
    items.add(index, item);
    if(objects!=null) objects.add(index, null);
  }

  public void remove(int index)
  {
    items.remove(index);
    if(objects!=null) objects.remove(index);
  }

  public void remove(String item)
  {
    for (int i=items.size()-1; i>=0; i--){
      String str=(String)items.get(i);
      if(str==item || str!=null && str.equals(item)){
        items.remove(i);
        if(objects!=null) objects.remove(i);
        break;
      }
    }
  }

  public int indexOf(String item)
  {
    return items.indexOf(item);
  }
  public int find(String item){
    int sb,se,sp,flag;

    sb=0;se=items.size()-1;flag=-1;
    while(sb<=se){
      sp=(sb+se) >> 1;
      flag=compare(item,(String)items.get(sp));
      if(flag>0) sb=sp+1;
      else if(flag<0) se=sp-1;
      else {
        sb=sp;break;
      }
    }
    return flag==0?sb:-sb-1;
  }
  public int addsort(String item){
    int index=find(item);
    if(index<0){
      items.add(-index-1,item);
      if(objects!=null) objects.add(-index-1, null);
    }
    return index;
  }
  public int indexOfObject(Object obj){
    return objects==null ? -1 : objects.indexOf(obj);
  }

  protected void quicksort(int L, int R){
    int I,J;
    do{
      I = L;
      J = R;
      String zfn = (String)items.get((L+R)>>1);
      do{
        while(compare(zfn,(String)items.get(I))>0) I++;
        while(compare(zfn,(String)items.get(J))<0) J--;
        if(I <= J){
          Object T;
          T=items.get(I);items.set(I,items.get(J));items.set(J,T);
          if(objects!=null){
            T=objects.get(I);objects.set(I,objects.get(J));objects.set(J,T);
          }
          I++;J--;
        }
      }while(I <= J);
      if(L < J) quicksort(L, J);
      L = I;
    }while(I < R);
  }
  public void sort(){
    if (items.size() > 1)
      quicksort(0,items.size()-1);
  }

  public String getText()
  {
    String text= new String("");
    for (int i=0;i<items.size();i++)
    {
      text=text+this.get(i)+"\n";
    }
    return text;
  }

  public void setText(String newText)
  {
    java.util.StringTokenizer st = new StringTokenizer(newText, "\n");
    items.clear();
    while (st.hasMoreTokens())
    {
      items.add(st.nextToken());
    }
    objects=null;
  }

  public static int compare(String s1, String s2){
    return s1==null?(s2==null?0:-1):s1.compareTo(s2);
  }
  public static void main(String[] args){
    StringList sl = new StringList();
    sl.add("1");
    sl.add("3");
    sl.add("2");
    sl.sort();
    System.out.println(sl.indexOf("3"));
    System.out.println(sl.getText());
    StringList s2 = new StringList();
    s2.assign(sl);
    System.out.println(s2.getText());
  }
}